//
//  BCSwitchView.m
//  NiuBanJinGold
//
//  Created by Billy on 15/10/14.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "BCSwitchView.h"
#import <objc/runtime.h>

@interface BCSwitchView ();

@property (nonatomic, strong) UIView *view_scroll; //底部滚动view

@end

@implementation BCSwitchView

- (instancetype)initWithFrame:(CGRect)frame Items:(NSArray *)items {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.color_title = [UIColor blackColor];
        self.font_title = [UIFont systemFontOfSize:15.0];
        self.color_scroll = [UIColor redColor];
        
        [self regsiterKVO];
        
        [self configureViewWithFrame:frame Items:items];
    }
    
    return self;
}

#pragma mark - Initial method

- (void) configureViewWithFrame:(CGRect ) frame Items:(NSArray *)items{
    
    //创建button
    for (int i = 0; i < items.count ; i ++) {
        
        CGRect frame_button = CGRectMake(frame.size.width/items.count * i,0 ,frame.size.width/items.count, self.bounds.size.height);
        
        
        UIButton *tmp = [self buttonCreatonWithTitle:items[i] frame:frame_button fontName:self.font_title titleColor:self.color_title];
        
        [tmp setTag:i+100];
        [self addSubview:tmp];
    }
    
    //底部红线
    UIView *view_tmp = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 2.0, frame.size.width/items.count, 2.0)] ;
    
    self.view_scroll = view_tmp;
    
    [view_tmp setTag:200];
    
    [view_tmp setBackgroundColor:self.color_scroll];
    
    [self addSubview:view_tmp];
}


- (UIButton *) buttonCreatonWithTitle:(NSString *) title frame:(CGRect ) frame  fontName:(UIFont *) fontName titleColor:( UIColor *) titleColor {
    
    UIButton *tmp = [[UIButton alloc] initWithFrame:frame];
    [tmp setTitle:title forState:UIControlStateNormal];
    [tmp.titleLabel setFont:fontName];
    [tmp setTitleColor:titleColor forState:UIControlStateNormal];
    
    [tmp addTarget:self action:@selector(buttonSelectedInCustomView:) forControlEvents:UIControlEventTouchUpInside];
    
    return tmp;
}

#pragma mark - Button events handling

- (void) buttonSelectedInCustomView: (UIButton *) button {
    
    //底部红线条跟随不同button移动
    
    UIView *tmp = [button.superview viewWithTag:200];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [UIView animateWithDuration:0.2 animations:^{
                
                tmp.frame = CGRectMake(button.frame.origin.x, tmp.frame.origin.y, tmp.bounds.size.width, tmp.bounds.size.height);
            }];
        });
    });
    
    [self.delegate buttonSelected:button];
}

- (void) buttonSelected:(UIButton *) button {
    
    NSLog(@"parent view called..");
}

#pragma mark - Lay out views

- (void)layoutSubviews { //100个我去写100个默认值比对？?
    
    if (self.color_scroll != [UIColor redColor]) { //只有颜色不同于默认值的时候才改变
        
        [self.view_scroll setBackgroundColor:self.color_scroll];
    }
    
    if (self.color_title != [UIColor blackColor] || (self.font_title != [UIFont systemFontOfSize:15.0]) ) {
        
        for (UIView *tmp in [self subviews]) {
            
            if ([tmp isMemberOfClass:[UIButton class]]){
                
                UIButton *button_sub = (UIButton *) tmp;
                
                [button_sub setTitleColor:self.color_title forState:UIControlStateNormal];
                [button_sub.titleLabel setFont:self.font_title];
            }
        }
    }
}

#pragma mark - KVO

- (void) regsiterKVO {
    
    for (NSString *key in [self allProperties]) {
        
        [self addObserver:self forKeyPath:key options:NSKeyValueObservingOptionNew context:nil];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    [self setNeedsLayout];
}

- (void)unregisterFromKVO {
    
    for (NSString *key in [self allProperties]) {
        
        [self removeObserver:self forKeyPath:key];
    }
}

#pragma mark - Device object all properties

- (NSArray *) allProperties{
    
    NSMutableArray *array = [NSMutableArray array];
    
    unsigned int outCount = 0;
    
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    
    for (int i = 0 ; i < outCount ; i ++){
        
        objc_property_t property = properties[i];
        
        NSString *str_propertyName = [NSString stringWithUTF8String:property_getName(property)];
        
        if(![str_propertyName isEqualToString:@"delegate"]){
            
            [array addObject:str_propertyName];
        }
    }
    
    free(properties);
    
    return array;
}

#pragma mark - KVO release

- (void)dealloc {
    
    [self unregisterFromKVO];
}

@end
